/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.service.impl;

import com.dejax.domain.BaseSysProjinfo;
import com.dejax.exception.BadRequestException;
import com.dejax.utils.*;
import lombok.RequiredArgsConstructor;
import com.dejax.repository.BaseSysProjinfoRepository;
import com.dejax.service.BaseSysProjinfoService;
import com.dejax.service.dto.BaseSysProjinfoDto;
import com.dejax.service.dto.BaseSysProjinfoQueryCriteria;
import com.dejax.service.mapstruct.BaseSysProjinfoMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Richer
* @date 2021-12-22
**/
@Service
@RequiredArgsConstructor
public class BaseSysProjinfoServiceImpl implements BaseSysProjinfoService {

    private final BaseSysProjinfoRepository baseSysProjinfoRepository;
    private final BaseSysProjinfoMapper baseSysProjinfoMapper;
    private final RedisUtils redisUtils;
    private String ProjinfonameRedisKey =  "sysprojinfoqueryall::";

    @Override
    public Map<String,Object> queryAll(BaseSysProjinfoQueryCriteria criteria, Pageable pageable){
        criteria.setFlag(0);
        Page<BaseSysProjinfo> page = baseSysProjinfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(baseSysProjinfoMapper::toDto));
    }

    @Override
    public List<BaseSysProjinfoDto> queryAll(BaseSysProjinfoQueryCriteria criteria){
        List<BaseSysProjinfoDto> baseSysProjinfoDtos = redisUtils.getList(ProjinfonameRedisKey+criteria.getProjname());
        if(baseSysProjinfoDtos != null ){
            return baseSysProjinfoDtos;
        }
        criteria.setFlag(0);
        List<BaseSysProjinfoDto> result =  baseSysProjinfoMapper.toDto(baseSysProjinfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
        if(result.size()>0){
            redisUtils.set(ProjinfonameRedisKey+criteria.getProjname(),result,60*60*2);
        }
        return result;
    }

    @Override
    @Transactional
    public BaseSysProjinfoDto findById(String projid) {
        BaseSysProjinfo baseSysProjinfo = baseSysProjinfoRepository.findById(projid).orElseGet(BaseSysProjinfo::new);
        ValidationUtil.isNull(baseSysProjinfo.getProjid(),"BaseSysProjinfo","projid",projid);
        return baseSysProjinfoMapper.toDto(baseSysProjinfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseSysProjinfoDto create(BaseSysProjinfo resources) {
        //查重
        BaseSysProjinfo baseSysProjinfo = baseSysProjinfoRepository.findOneByProjnameAndCode(resources.getProjname(),resources.getProjcode());
        if(baseSysProjinfo != null){
            throw new BadRequestException(BAD_REQUEST, "项目编码/项目名称 已存在");

        }
        resources.setProjid(IdUtil.simpleUUID());
        resources.setFlag(0);
        //清除缓存
        redisUtils.delByPrefix(ProjinfonameRedisKey);
        return baseSysProjinfoMapper.toDto(baseSysProjinfoRepository.save(resources));
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BaseSysProjinfo resources) {
        BaseSysProjinfo baseSysProjinfo = baseSysProjinfoRepository.findById(resources.getProjid()).orElseGet(BaseSysProjinfo::new);
        ValidationUtil.isNull( baseSysProjinfo.getProjid(),"BaseSysProjinfo","id",resources.getProjid());
        baseSysProjinfo.copy(resources);
        baseSysProjinfoRepository.save(baseSysProjinfo);
        redisUtils.delByPrefix(ProjinfonameRedisKey);

    }

    @Override
    public void deleteAll(String[] ids) {
        BaseSysProjinfo updateSource = new BaseSysProjinfo();
        for (String projid : ids) {
            //改为 update
            updateSource.setProjid(projid);
            updateSource.setFlag(1);
            this.update(updateSource);
        }
    }

    @Override
    public void download(List<BaseSysProjinfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BaseSysProjinfoDto baseSysProjinfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目编码", baseSysProjinfo.getProjcode());
            map.put("项目名称", baseSysProjinfo.getProjname());
            map.put("建设单位", baseSysProjinfo.getBuildunit());
            map.put("项目所在地区", baseSysProjinfo.getInarea());
            map.put(" flag",  baseSysProjinfo.getFlag());
            map.put(" notes",  baseSysProjinfo.getNotes());
            map.put(" reguser",  baseSysProjinfo.getReguser());
            map.put(" regtime",  baseSysProjinfo.getRegtime());
            map.put(" moduser",  baseSysProjinfo.getModuser());
            map.put(" modtime",  baseSysProjinfo.getModtime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}